package com.keat.qqclient.ui;

import com.keat.qqclient.service.FileClientService;
import com.keat.qqclient.service.MessageClientService;
import com.keat.qqclient.service.UserClientService;
import javafx.collections.ObservableMap;
import javafx.event.ActionEvent;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javafx.stage.FileChooser;

public class UserClientController {
    public TextField text_username;
    public TextField text_password;
    public Button btn_login;
    public Button btn_logout;
    public TextArea text_record;
    public ListView list_user;
    public TextField text_message;
    public Button btn_message;
    public Button btn_message_mass;
    public Button btn_file;
    public Button btn_list_user;
    public String content;

    private UserClientService userClientService = new UserClientService();
    private MessageClientService messageClientService = new MessageClientService();
    private FileClientService fileClientService = new FileClientService();
    public HashMap<String, String> chatdata = new HashMap<>();
    FileChooser fil_chooser = new FileChooser();

    public static String getByDate(){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public void login(MouseEvent mouseEvent) {
        if(userClientService.checkUser(text_username.getText(), text_password.getText(), this)) {
            Stage stage = (Stage)btn_login.getScene().getWindow();
            stage.setTitle("已登录 - " + text_username.getText());
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("登录成功");
            alert.setHeaderText(null);
            alert.setContentText("登陆成功");
            alert.show();
        }
        else {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("登录失败");
            alert.setHeaderText(null);
            alert.setContentText("登录失败");
            alert.show();
        }
    }

    public void logout(MouseEvent mouseEvent) {
        userClientService.logout();
        Stage stage = (Stage)btn_logout.getScene().getWindow();
        stage.setTitle("未登录");
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("登出成功");
        alert.setHeaderText(null);
        alert.setContentText("登出成功");
        alert.show();
    }

    public void getUserList(MouseEvent mouseEvent) {
        userClientService.onlineFriendList();
    }

    public void selectUser(MouseEvent mouseEvent) {
        String userId = list_user.getSelectionModel().getSelectedItem().toString();
        String records = chatdata.get(userId);
        text_record.setText(records);
        System.out.println(records);
    }

    public void sendMessage(MouseEvent mouseEvent) {
        content = text_message.getText();
        if(content.length() == 0) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("发送失败");
            alert.setHeaderText(null);
            alert.setContentText("不允许发送空内容");
            alert.show();
        }
        else {
            String userId = list_user.getSelectionModel().getSelectedItem().toString();
            if(text_username.getText().equals(userId))
            {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("发送失败");
                alert.setHeaderText(null);
                alert.setContentText("不允许向给自己发送消息");
                alert.show();
            }
            else {
                messageClientService.sendMessageToOne(content, text_username.getText(), userId);
                String records = chatdata.get(userId);
                records += "\n\n[" + getByDate() + "] " + text_username.getText() + ":\n" + content;
                chatdata.put(userId, records);
                text_record.clear();
                text_record.setText(records);
            }
        }
    }

    public void sendMessMessage(MouseEvent mouseEvent) {
        String userId = list_user.getSelectionModel().getSelectedItem().toString();
        content = text_message.getText();
        if(content.length() == 0) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("发送失败");
            alert.setHeaderText(null);
            alert.setContentText("不允许发送空内容");
            alert.show();
        }
        else {
            messageClientService.sendMessageToAll(content, text_username.getText());
            for (Map.Entry<String, String> entry : chatdata.entrySet()) {
                String value = entry.getValue();
                value += "\n\n[" + getByDate() + "] [广播] " + text_username.getText() + ":\n" + content;
                entry.setValue(value);
            }
            String records = chatdata.get(userId);
            text_record.setText(records);
        }
    }

    public void sendFile(MouseEvent mouseEvent) {
        String userId = list_user.getSelectionModel().getSelectedItem().toString();
        if(text_username.getText().equals(userId))
        {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("发送失败");
            alert.setHeaderText(null);
            alert.setContentText("不允许向给自己发送文件");
            alert.show();
        }
        else {
            Stage stage = (Stage) btn_login.getScene().getWindow();
            File file = fil_chooser.showOpenDialog(stage);
            if (file != null) {
                File fileObj = file.getAbsoluteFile();
                String filePath = fileObj.toString();
                fileClientService.sendFileToOne(filePath, fileObj.getName(), text_username.getText(), userId);
                String records = chatdata.get(userId);
                records += "\n\n[" + getByDate() + "] " + text_username.getText() + ":\n[传输文件] " + filePath;
                chatdata.put(userId, records);
                text_record.setText(records);
            }
        }
    }
}
